package cn.ac.iscas.action.paper;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.paper.ConferenceType;
import cn.ac.iscas.bean.paper.ConfererncePaper;
import cn.ac.iscas.bean.paper.JournalLevel;
import cn.ac.iscas.bean.paper.JournalPaper;
import cn.ac.iscas.bean.paper.Paper;
import cn.ac.iscas.service.paper.IConferencePaperService;
import cn.ac.iscas.service.paper.IJournalServive;
import cn.ac.iscas.service.paper.IPaperService;

@Controller @Scope("prototype")
public class PaperFormatPrint {
	private String[] paperId;
	@Resource private IPaperService paperService;
	@Resource private IJournalServive journalPaperService;
	@Resource private IConferencePaperService conferencePaperService;
	private QueryCondition queryCondition;
	private int page;
	public String showPrintView()
	{
		page = (0==page)?1:page;
		PageView<Paper> pageView = new PageView<Paper>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("publicationYear", "desc");
		pageView.setQueryResult(paperService.getScrollData(Paper.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "showPrintView";
	}
	public String query()
	{
		if(page<=0)page=1;
	    StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		if(queryCondition.getPaperTittle()!=null && !"".equals(queryCondition.getPaperTittle())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.paperTittle like ?");
			params.add("%"+ queryCondition.getPaperTittle()+ "%");
		}
		if(queryCondition.getKeyWord()!=null && !"".equals(queryCondition.getKeyWord())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.keyWord like ?");
			params.add("%"+ queryCondition.getKeyWord()+ "%");
		}
		if(queryCondition.getPublicationYear()!=null && !"".equals(queryCondition.getPublicationYear())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.publicationYear = ?");
			params.add(queryCondition.getPublicationYear());
			
			//	int year = Integer.parseInt(queryCondition.getPublicationYear());
		//	System.out.println(year);
		//	Calendar beginDate = Calendar.getInstance();
		//	beginDate.set(year, 0, 1);
		//	int nextYear = year+1;
		//	System.out.println(nextYear);
		//	Calendar endDate = Calendar.getInstance();
		//	endDate.set(nextYear, 0, 1);
		//  System.out.println(beginDate);
	//	    System.out.println(endDate);
		//	if(params.size()>0) jpql.append(" and ");
		//	System.out.println(queryCondition.getPublicationYear()+"getPublicationYear"+"\n");
		//	jpql.append(" o.publicationYear >= ?");
		//	params.add(beginDate);
		//	jpql.append(" and o.publicationYear < ?");
		//	params.add(endDate);
			
		}
		if(queryCondition.getAuthor()!=null && !"".equals(queryCondition.getAuthor())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" ((o.firstAuthor like ? ");
			params.add("%"+queryCondition.getAuthor()+"%");
			jpql.append("or o.secondAuthor like ? ");
			params.add("%"+queryCondition.getAuthor()+"%");
			jpql.append("or o.thirdAuthor like ? ");
			params.add("%"+queryCondition.getAuthor()+"%");
			jpql.append("or o.fourthAuthor like ? ");
			params.add("%"+queryCondition.getAuthor()+"%");
			jpql.append("or o.fifthAuthor like ? ");
			params.add("%"+queryCondition.getAuthor()+"%");
			jpql.append("or o.sixthAuthor like ? )) ");
			params.add("%"+queryCondition.getAuthor()+"%");
		}
		if(queryCondition.getFirstAuthor()!=null && !"".equals(queryCondition.getFirstAuthor())){
			System.out.println(queryCondition.getFirstAuthor());
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.firstAuthor like ?");
			params.add("%"+queryCondition.getFirstAuthor()+"%");
		}
		if(queryCondition.getJournal()!=null && !"".equals(queryCondition.getJournal()))
		{
			if("一级期刊".equals(queryCondition.getJournal()))
			{
				if(params.size()>0) jpql.append(" and ");
				jpql.append(" o.journalLevel like ?");
				params.add(JournalLevel.一级);
			}
			else if("核心期刊".equals(queryCondition.getJournal()))
			{
				if(params.size()>0) jpql.append(" and ");
				jpql.append(" o.journalLevel like ?");
				params.add(JournalLevel.核心);
			}
			else if("国际期刊".equals(queryCondition.getJournal()))
			{
				if(params.size()>0) jpql.append(" and ");
				jpql.append(" o.journalLevel like ?");
				params.add(JournalLevel.国际);
			}
			PageView<JournalPaper> pageView=new PageView<JournalPaper>(7,page);
			LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
			orderby.put("publicationYear", "desc"); 
			pageView.setQueryResult(journalPaperService.getScrollData(JournalPaper.class,pageView.getFirstResult(), 
					pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
			ActionContext.getContext().put("pageView", pageView);
		}
		else if(queryCondition.getConferencePaper()!=null && !"".equals(queryCondition.getConferencePaper()))
		{
			if("国内会议".equals(queryCondition.getConferencePaper()))
			{
				if(params.size()>0) jpql.append(" and ");
				jpql.append(" o.conferenceType like ?");
				params.add(ConferenceType.国内);
			}
			
			else if("国际会议".equals(queryCondition.getConferencePaper()))
			{
				if(params.size()>0) jpql.append(" and ");
				jpql.append(" o.conferenceType like ?");
				params.add(ConferenceType.国际);
			}
			
			PageView<ConfererncePaper> pageView=new PageView<ConfererncePaper>(7,page);
			LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
			orderby.put("publicationYear", "desc"); 
			pageView.setQueryResult(conferencePaperService.getScrollData(ConfererncePaper.class,pageView.getFirstResult(), 
					pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
			ActionContext.getContext().put("pageView", pageView);
		}
		else
		{
			PageView<Paper> pageView=new PageView<Paper>(7,page);
			LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
			orderby.put("publicationYear", "desc"); 
			pageView.setQueryResult(paperService.getScrollData(Paper.class,pageView.getFirstResult(), 
					pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
			ActionContext.getContext().put("pageView", pageView);
		}
		return "showPrintView";
	}
	public String print()
	{
	    Set<ConfererncePaper> conferencePapers = new HashSet<ConfererncePaper>();
	    Set<JournalPaper>  journalPapers = new HashSet<JournalPaper>();
		if(paperId != null)
		{
			for(String id:paperId)
			{
				Paper paper = paperService.get(Paper.class,Integer.valueOf(id));
				if(paper instanceof ConfererncePaper)
					conferencePapers.add((ConfererncePaper)paper);
				else 
					journalPapers.add((JournalPaper)paper);
			}
		}
		List<String> conferencePaperPrints = makeConfererncePage(conferencePapers);
		List<String> journalPaperPrints = makeJournalPage(journalPapers);
		ActionContext.getContext().put("journalPaperPrints", journalPaperPrints);
		ActionContext.getContext().put("conferencePaperPrints", conferencePaperPrints);
		return "printView";
	}
	public String[] getPaperId() {
		return paperId;
	}
	public void setPaperId(String[] paperId) {
		this.paperId = paperId;
	}
	public IPaperService getPaperService() {
		return paperService;
	}
	public void setPaperService(IPaperService paperService) {
		this.paperService = paperService;
	}
	public IJournalServive getJournalPaperService() {
		return journalPaperService;
	}
	public void setJournalPaperService(IJournalServive journalPaperService) {
		this.journalPaperService = journalPaperService;
	}
	public IConferencePaperService getConferencePaperService() {
		return conferencePaperService;
	}
	public void setConferencePaperService(
			IConferencePaperService conferencePaperService) {
		this.conferencePaperService = conferencePaperService;
	}
	public QueryCondition getQueryCondition() {
		return queryCondition;
	}
	public void setQueryCondition(QueryCondition queryCondition) {
		this.queryCondition = queryCondition;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	private List<String> makeConfererncePage(final Set<ConfererncePaper> paperSet){
		DateFormat f1 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA);
		DateFormat f2 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.ENGLISH);
		List<String> list = new ArrayList<String>();
        for(ConfererncePaper paper:paperSet){
        	StringBuffer sb = new StringBuffer();
//            sb.append(i+1);
//            sb.append("&nbsp;&nbsp;");
            if(paper.getFirstAuthor()!=null && !"".equals(paper.getFirstAuthor())){
                sb.append(paper.getFirstAuthor());
                sb.append(",");
            }
            if(paper.getSecondAuthor()!=null && !"".equals(paper.getSecondAuthor())){
                sb.append(paper.getSecondAuthor());
                sb.append(",");
            }
            if(paper.getThirdAuthor()!=null && !"".equals(paper.getThirdAuthor())){
                sb.append(paper.getThirdAuthor());
                sb.append(",");
            }
            if(paper.getFourthAuthor()!=null && !"".equals(paper.getFourthAuthor())){
                sb.append(paper.getFourthAuthor());
                sb.append(",");
            }
            if(paper.getFifthAuthor()!=null && !"".equals(paper.getFifthAuthor())){
                sb.append(paper.getFifthAuthor());
                sb.append(",");
            }
            if(paper.getSixthAuthor()!=null && !"".equals(paper.getSixthAuthor())){
                sb.append(paper.getSixthAuthor());
                sb.append(",");
            }
            sb.append("\"").append(paper.getPaperTittle()).append("\"").append(",");
            sb.append(paper.getConferenceShortName()).append(",");
            sb.append(paper.getConferenceAddress()).append(",");
            if(paper.getConferenceStartTime() != null && paper.getConferenceEndTime() != null){
            	if(paper.getConferenceType() == ConferenceType.国际){
            	
            		sb.append(f2.format(paper.getConferenceStartTime().getTime())).append("-")
            		.append(f2.format(paper.getConferenceEndTime().getTime())) ;
            	}else{
            		sb.append(f1.format(paper.getConferenceStartTime().getTime())).append("-")
            		.append(f1.format(paper.getConferenceEndTime().getTime())) ;
            	}
            	sb.append(",");
            }
            
            sb.append("pp.").append(paper.getStartPage()).append("~").append(paper.getEndPage()).append(".");
            list.add(sb.toString());
		}
		return list;
	}
	private List<String> makeJournalPage(final Set<JournalPaper> paperSet){
		List<String> list = new ArrayList<String>();
		for(JournalPaper paper:paperSet){
			StringBuffer sb = new StringBuffer();
//            sb.append(i+1);
//            sb.append("&nbsp;&nbsp;");
            if(paper.getFirstAuthor()!=null && !"".equals(paper.getFirstAuthor())){
                sb.append(paper.getFirstAuthor());
                sb.append(",");
            }
            if(paper.getSecondAuthor()!=null && !"".equals(paper.getSecondAuthor())){
                sb.append(paper.getSecondAuthor());
                sb.append(",");
            }
            if(paper.getThirdAuthor()!=null && !"".equals(paper.getThirdAuthor())){
                sb.append(paper.getThirdAuthor());
                sb.append(",");
            }
            if(paper.getFourthAuthor()!=null && !"".equals(paper.getFourthAuthor())){
                sb.append(paper.getFourthAuthor());
                sb.append(",");
            }
            if(paper.getFifthAuthor()!=null && !"".equals(paper.getFifthAuthor())){
                sb.append(paper.getFifthAuthor());
                sb.append(",");
            }
            if(paper.getSixthAuthor()!=null && !"".equals(paper.getSixthAuthor())){
                sb.append(paper.getSixthAuthor());
                sb.append(",");
            }
            if(paper.getJournalLevel() == JournalLevel.国际){
            	sb.append("\"").append(paper.getPaperTittle()).append("\"").append(",");
            	sb.append(paper.getPublicationJournal()).append(".");
            	sb.append("Vol.").append(paper.getVolume()).append(",").append("No.").append(paper.getIssue()).append(",pp");
            }
            else {
            	sb.append(paper.getPaperTittle()).append(".");
            	sb.append(paper.getPublicationJournal()).append(",");
            	sb.append(paper.getPublicationYear()).append(",");
            	sb.append(paper.getVolume()).append("(").append(paper.getIssue()).append(")").append(":");
            }
            sb.append(paper.getStartPage()).append("~").append(paper.getEndPage()).append(".");
            list.add(sb.toString());
		}
		return list;
	}
}
